package com.yaojiaxun.logisticsPlatformSystem.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yaojiaxun.logisticsPlatformSystem.dto.NewsDTO;
import com.yaojiaxun.logisticsPlatformSystem.dto.PageDTO;
import com.yaojiaxun.logisticsPlatformSystem.entity.News;
import com.yaojiaxun.logisticsPlatformSystem.mapper.NewsMapper;
import com.yaojiaxun.logisticsPlatformSystem.util.SnowFlakeIDWorker;
import com.yaojiaxun.logisticsPlatformSystem.util.TimeFormat;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Auther: yjx_j
 * @Date: 2019/2/19 20:10
 * @Description:
 */
@Service
public class NewsService {
    @Autowired
    private NewsMapper newsMapper;

    public News selectNews(Long newsId){
        return newsMapper.findNewsByNewsId(newsId);
    }

    public boolean addNews(News news){
        news.setNewsId(new SnowFlakeIDWorker(8, 1).nextId());
        news.setCreateTime(Instant.now().toEpochMilli());
        news.setUpdateTime(Instant.now().toEpochMilli());
        return newsMapper.addNews(news)>0;
    }

    public boolean deleteNews(Long newsId){
        return newsMapper.deleteNewsByNewsId(newsId)>0;
    }

    public boolean updateNews(News news){
        news.setUpdateTime(Instant.now().toEpochMilli());
        return newsMapper.updateNews(news)>0;
    }

    public NewsDTO beanToDTO(News news){
        if (news == null){
            return null;
        }
        NewsDTO newsDTO = new NewsDTO();
        BeanUtils.copyProperties(news, newsDTO);
        newsDTO.setCreateTime(TimeFormat.convertTime2(news.getCreateTime()));
        newsDTO.setUpdateTime(TimeFormat.convertTime2(news.getUpdateTime()));
        return newsDTO;
    }

    public PageDTO<NewsDTO> findPage(Integer pageNum){
        PageDTO<NewsDTO> pageDTO = new PageDTO<>();
        Page<NewsDTO> page = new Page<>(pageNum, 10);
        List<NewsDTO> results = newsMapper.selectPage(page, getWrapper()).stream().map(this::beanToDTO).collect(Collectors.toList());
        pageDTO.setResult(results);
        pageDTO.setPage(page);
        return pageDTO;
    }

    //按最近时间显示新闻资讯
    private Wrapper<News> getWrapper(){
        Wrapper<News> wrapper = new EntityWrapper<>();
        wrapper.orderBy("update_time", false);
        return wrapper;
    }

    public List<NewsDTO> findLast6News(){
        List<News> news = newsMapper.findLast6News();
        List<NewsDTO> list = new ArrayList<>();
        for (int i =0; i<news.size(); i++){
            NewsDTO newsDTO = beanToDTO(news.get(i));
            newsDTO.setUpdateTime(TimeFormat.convertTime(news.get(i).getUpdateTime()));
            list.add(newsDTO);
        }
        return list;
    }

    public List<NewsDTO> findOther6News(){
        List<News> news = newsMapper.findOther6News();
        List<NewsDTO> list = new ArrayList<>();
        for (int i =0; i<news.size(); i++){
            NewsDTO newsDTO = beanToDTO(news.get(i));
            newsDTO.setUpdateTime(TimeFormat.convertTime(news.get(i).getUpdateTime()));
            list.add(newsDTO);
        }
        return list;
    }

    public NewsDTO findNewsInfoTimeFormat(Long newsId){
        News news = newsMapper.findNewsByNewsId(newsId);
        NewsDTO newsDTO = new NewsDTO();
        BeanUtils.copyProperties(news, newsDTO);
//        String str = news.getNewsContent();
//        char[] ch = str.toCharArray();
//        for (char c : ch) {
//            if (c == '\n'){
//
//            }
//        }
        newsDTO.setUpdateTime(TimeFormat.convertTime3(news.getUpdateTime()));
        return newsDTO;
    }
}
